package com.hhdys.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhdys.dao.PositionMapper;
import com.hhdys.model.Position;
import com.hhdys.model.PositionExample;

@Service
public class PositionServiceImpl implements PositionService {
	@Autowired
	private PositionMapper dao;

	@Override
	public List<Position> selPositions() {
		return dao.selectByExample(null);
	}

	@Override
	public boolean addPosition(String name, String addUser) {
		Position position = new Position();
		position.setName(name);
		position.setLastModifier(addUser);
		position.setLastModifyTime(new Date());
		return dao.insert(position) > 0;
	}

	@Override
	public boolean editPosition(int id, String name, String modifyUser) {
		Position position = new Position();
		position.setId(id);
		position.setName(name);
		position.setLastModifier(modifyUser);
		position.setLastModifyTime(new Date());
		return dao.updateByPrimaryKeySelective(position) > 0;
	}

	@Override
	public boolean delPosition(String ids) {
		String[] idsStr = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String str : idsStr) {
			list.add(NumberUtils.toInt(str));
		}
		PositionExample example = new PositionExample();
		PositionExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(list);
		return dao.deleteByExample(example) > 0;
	}

}
